package com.ctgu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctgu.server.dao.constant.CacheKey;
import com.ctgu.server.dao.dto.AttendanceTimeSlotDto;
import com.ctgu.server.dao.entity.AttendanceTimeSlot;
import com.ctgu.server.dao.vo.Result;
import com.ctgu.server.mapper.AttendanceTimeSlotMapper;
import com.ctgu.server.service.AttendanceTimeSlotService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.server.utils.ResultUtil;
import com.github.dozermapper.core.Mapper;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 打卡时间段表 服务实现类
 * </p>
 *
 * @author ctgu
 * @since 2023-06-13
 */
@Service
public class AttendanceTimeSlotServiceImpl extends ServiceImpl<AttendanceTimeSlotMapper, AttendanceTimeSlot> implements AttendanceTimeSlotService {
    /**
    * 打卡时间段数量限制
    * */
    private static final int TimeSlot_Size=10;
    @Autowired
    private Mapper mapper;
    @Autowired
    private AttendanceGroupUserServiceImpl attendanceGroupUserService;

    @Autowired
    private CacheChannel cacheChannel;
    @Override
    @Transactional
    public Result addAttendanceTimeSlot(AttendanceTimeSlotDto attendanceTimeSlotDto) {
        if (!attendanceGroupUserService.is_admin(attendanceTimeSlotDto.getOpId(),attendanceTimeSlotDto.getAttGrId())) {
            return ResultUtil.error("权限不足");
        }

        //查询数据是否超出要求
        List<AttendanceTimeSlot> attendanceTimeSlotList=attendanceTimeSlotDto.getAttendanceTimeSlotList();
        LambdaQueryWrapper<AttendanceTimeSlot> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttendanceTimeSlot::getGroupId,attendanceTimeSlotList);

        if(attendanceTimeSlotList.size()+this.count(lambdaQueryWrapper)>TimeSlot_Size ){
            return ResultUtil.error("打卡时间段数量不能超过："+TimeSlot_Size+"!");
        }
        String g=attendanceTimeSlotDto.getOpId();
        attendanceTimeSlotList.stream().map(s->{s.setGroupId(g);
        return  null;});

        this.saveBatch(attendanceTimeSlotList);
        return ResultUtil.success("请求成功！");
    }

    @Override
    @Transactional
    public boolean deleteAll(String opId, String attGrId) {
       if(attendanceGroupUserService.is_admin(opId,attGrId)){
           return false;
       }
       LambdaQueryWrapper<AttendanceTimeSlot> lambdaQueryWrapper=new LambdaQueryWrapper<>();
       lambdaQueryWrapper.eq(AttendanceTimeSlot::getGroupId,attGrId);

       return this.remove(lambdaQueryWrapper);
    }
    /**
     * @param userId:
     * @param attGrId:
     * @return List<AttendanceTimeSlot>
     * @author kvzjj
     * @description TODO 查询当前考勤组所有时间段信息
     * @date 2023/6/13 20:40
     */
    @Override
    public List<AttendanceTimeSlot> listAllTimeSlot(String userId, String attGrId) {
        if(attendanceGroupUserService.is_admin(userId,attGrId)){
            return null;
        }
        //1.查询缓存中是否有对应的数据
        List<AttendanceTimeSlot> attendanceTimeSlotList = (List<AttendanceTimeSlot>) cacheChannel.get(CacheKey.ATTENDANCE_TIMESLOT,attGrId).getValue();
        if(attendanceTimeSlotList==null){
            //2.查询数据库
            LambdaQueryWrapper<AttendanceTimeSlot> attendanceTimeSlotLambdaQueryWrapper=new LambdaQueryWrapper<>();
            attendanceTimeSlotLambdaQueryWrapper.eq(AttendanceTimeSlot::getGroupId,attGrId);
            attendanceTimeSlotList= this.list(attendanceTimeSlotLambdaQueryWrapper);
            cacheChannel.set(CacheKey.ATTENDANCE_TIMESLOT,attGrId,attendanceTimeSlotList);
        }
        //3.直接返回数据
        return attendanceTimeSlotList;
    }
}
